/*键值对（“key = value”）字符串，在开发中经常使用
	要求1：请自己定义一个接口，实现根据key获取.
	要求2：编写测试用例。
	要求3：键值对中间可能有n多空格，请去除空格

	注意：键值对字符串格式可能如下：
	"key1 = value1"
	"key2 =       value2"
	"key3  = value3"
	"key4        = value4"
	"key5   =   "
	"key6   ="
	
	int getKeyByValue(char *keyvaluebuf,  char *keybuf,  char *valuebuf, int * valuebuflen);

	int main(void)
	{
		//...
		getKeyByValude("key1 =   valude1", "key1", buf, &len);
		//...

		return 0;
	}
*/
 */
# include<stdio.h>
# include<string.h>
# include<ctype.h>
int getKeyByValue(char *keyvaluebuf,  char *keybuf,  char *valuebuf, int * valuebuflen)
{
    if (keyvaluebuf == NULL)
            return -1;
    char *p = strstr(keyvaluebuf,"=")+1;
    if (*p == "\0")
        return -2;
    //char *q = strstr(keyvaluebuf,"\0");
    //printf ("%p,%p",p,q);
    int begin = 0;
    int end = strlen (p) -1;
    if (begin == end)
            return -2;
    while (isspace(p[begin]) && p[begin] != 0)
    {
        if (begin == end)
            return -2;
        //    return -2;
        begin++;

    }

    while (isspace(p[end]) && p[end] > 0)
    {
        if (begin == end)
            return -2;
        end--;
        
    }
    if (p[end] == 0)
        return -3;rm
    *valuebuflen = end -begin +1;
    strncpy(valuebuf,p+begin,*valuebuflen);
}

int main (void)
{
    char keyvaluebuf[]= "key6   =";
    char buf[100] = {0};
    int len = 0;
    char key[100] = {0};
    int num;
    strncpy(key,keyvaluebuf,4);
    //printf ("%s",key);
     num =getKeyByValue(keyvaluebuf, key, buf, &len);
    if (num == -2)
        printf ("请输入正确的键值\n");
    else
        printf ("%s对应的键值为%s\n",key,buf);

    return 0;
}
